$(function () {
  
    $.datepicker.setDefaults($.datepicker.regional["th"]);

});

function popitup(url, windowName, key) {
    if (key != "") { url = url + "?searchKey=" + key; }
    newwindow = window.open(url, windowName, 'height=600,width=500');
    if (window.focus) { newwindow.focus() }
    return false;
}

function SetDatePicker(controlId, maxdate, mindate, yearrange) {
    
    var obj = '#' + controlId;
    $(obj).attr('readonly', true);
    $(obj).datepicker({
        buttonImage: '@Url.Content("~/Content/images/datepicker.png")',
        changeMonth: true, changeYear: true,
        defaultDate: new Date(),
        maxDate: maxdate,
        minDate: mindate,
        yearRange: yearrange,
    });
}

function SetLookupFor(controlId, getLookupId, popLokupId) {
    
    var GetLookup_url = $("#"+getLookupId).val();
    var PopLookup_url = $("#"+popLokupId).val();

    var obj = '#' + controlId;
    var txtCode = obj + 'Code';
    var txtName = obj + 'Name';
    var btnObj = obj + 'Btn';

    $(txtCode).change(function () {
        var searchKey = $(txtCode).val();
        //alert(searchKey);
        $.ajax({
            url: GetLookup_url,
            type: 'GET',
            data: 'searchKey=' + searchKey,
            success: function (result) {
                if (result == "NotFound") {
                    $(obj).val("");
                    $(txtCode).val("");
                    $(txtName).val("");
                    popitup(PopLookup_url, obj, "");
                }
                else if (result == "MoreOne") {
                    $(obj).val("");
                    $(txtCode).val("");
                    $(txtName).val("");
                    popitup(PopLookup_url, obj, searchKey);
                }
                else {
                    var Binding = result.split("|");
                    $(obj).val(Binding[0]);
                    $(txtCode).val(Binding[1]);
                    $(txtName).val(Binding[2]);
                }
            },
            fail: function (result) {
                $(obj).val("");
                $(txtCode).val("");
                $(txtName).val("");
                popitup(PopLookup_url, obj, "");
            }
        });
    });

    $(btnObj).on("click", function () {
        popitup(PopLookup_url, obj, "");
    });
}